<template>
  <Container>
    <template slot="top">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="公告标题" prop="noticeTitle">
          <el-input
              v-model="queryParams.noticeTitle"
              placeholder="请输入公告标题"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="发布人" prop="sender">
          <el-select v-model="queryParams.sender" placeholder="发布人" clearable filterable >
            <el-option
                v-for="user in userSelectData"
                :key="user.value"
                :label="user.label"
                :value="user.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="阅读状态" prop="readFlag">
          <el-select v-model="queryParams.readFlag" placeholder="阅读状态" clearable>
            <el-option
                v-for="dict in dict.type.sys_readflag"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="公告类型" prop="noticeType">
          <el-select v-model="queryParams.noticeType" placeholder="公告类型" clearable>
            <el-option
                v-for="dict in dict.type.sys_notice_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="multiple"
              @click="handleRead"
          >全部标注已读</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:noticeSend:remove']"
          >批量删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </template>
    <template slot="center">
      <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange" :border="true" height="100%">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
        <el-table-column label="消息标题" align="center" prop="noticeTitle" :show-overflow-tooltip="true"/>
        <el-table-column label="消息类型" align="center" prop="noticeType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_notice_type" :value="scope.row.noticeType"/>
          </template>
        </el-table-column>
        <el-table-column label="发布人" align="center" prop="senderName" />
        <el-table-column label="发布时间" align="center" prop="sendTime" />
        <el-table-column label="优先级" align="center" prop="priority">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_priority" :value="scope.row.priority"/>
          </template>
        </el-table-column>
        <el-table-column label="阅读状态" align="center" prop="readFlag">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_readflag" :value="scope.row.readFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleSee(scope.row)"
                v-hasPermi="['system:noticeSend:list']"
            >查看</el-button>
            <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['workflow:category:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <template slot="bottom">
      <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
      />
    </template>
    <show-notice ref="showNotice" @ok="modalFormOk"></show-notice>
    <show-dynamic-notice ref="showDynamicNotice" :path="openPath" :formData="formData" />
  </Container>
</template>

<script>
import ShowNotice from '@/components/HeaderNotice/ShowNotice'
import ShowDynamicNotice from '@/components/HeaderNotice/ShowDynamicNotice'
import { getMyNoticeSend, readNotice, getNotice, updateNotice,delMyNotice } from "@/api/system/notice";
import { userSelect } from '@/api/system/user'

export default {
  name: "MyNotice",
  components: {
    ShowDynamicNotice,
    ShowNotice,
  },
  dicts: ['sys_readflag', 'sys_notice_type','sys_priority'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        noticeTitle: [
          { required: true, message: "公告标题不能为空", trigger: "blur" }
        ],
        noticeType: [
          { required: true, message: "公告类型不能为空", trigger: "change" }
        ]
      },
      formData: {},
      openPath: '',
      userSelectData:[]
    };
  },
  created() {
    this.getUserSelect();
    this.getList();
  },
  methods: {
    /**
     * 查询用户下拉数据
     */
    getUserSelect(){
      userSelect().then(response => {
        this.userSelectData=response.data;
      });
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      getMyNoticeSend(this.queryParams).then(res => {
        this.noticeList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /**
     * 已读
     * @param row
     */
    handleRead(row){
      const sendIds = row.sendId || this.ids;
      readNotice(sendIds).then(() => {
        this.getList();
      })
    },
    /** 删除 */
    handleDelete(row) {
      const sendIds = row.sendId || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + sendIds + '"的数据项？').then(() => {
        this.loading = true;
        return delMyNotice(sendIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /**
     * 取消按钮
     */
    cancel() {
      this.open = false;
      this.reset();
    },
    /**
     * 表单重置
     */
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0"
      };
      this.$resetForm("form");
    },
    handleSee(record) {
      if(record.readFlag == '0') {
        this.handleRead(record)
      }
      this.hovered = false;
      if (record.openType === 'component') {
        this.openPath = record.openPage;
        this.formData = {
          id: record.busId
        };
        this.$refs.showDynamicNotice.detail(record.openPage);
      } else {
        this.$refs.showNotice.detail(record);
      }
    },
    modalFormOk() {},
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$resetForm("queryForm");
      this.handleQuery();
    },
    /**
     * 多选框选中数据
     * @param selection
     */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sendId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    }
  }
};
</script>
